
import requests

from app.web.casesuit.get_token import  GetToken
from app.web.comm.check import Check
import json
from app.web.conf.config import  *
C=Check()

# base_url="https://shanghu-backend-dev.wetax.com.cn"
# token_pc="G8tEFub3YBtwpkfYD9u2t6zGBWr6DaNdlEb1rzuO"

class GetClient():
    requests.packages.urllib3.disable_warnings()  # Python的第三方库requests提示警告InsecureRequestWarning的问题
    # def __init__(self,tax_code,password):
    #     self.tax_code=tax_code
    #     self.password=password
    #
    #     global token_pc
    #     token_pc = GetToken().login_token(tax_code, password)
        # token_pc="xi37lPwwnU8Jje6OtmtV21MgvkShLelxr3n3ucU5"


    # def add_store(self,tax_code="GAPENE7ETXN3LED",password="zxz123456"):#添加商品编码,住宿服务
        # token_pc = GetToken().token_pc(tax_code,password)
    def project_details(self,token_pc):  #获取开票项

        url = base_url_cloud + '/v3/client-project/projects?token=%s'%token_pc
        print("获取开票项请求url：{}".format(url))
        request_data={"page":1,"firm_goods_no":"","firm_goods_name":"","tax_rate":"","page_size":10}
        headers = {"Accept": "application/json, text/plain, */*", "Content-Type": "application/json;charset=UTF-8"}

        response=requests.post(url=url,json=request_data, headers=headers, verify=False)
        print("获取开票项请求地址：%s" % url)
        # print(response.json())
        res = response.json()['data']['list']
        # print(res)
        return res

    def project_details_default(self,token_pc):  # 获取默认开票项目
        list1 = self.project_details(token_pc)
        for i in list1:
            if i['is_default'] == 1:
                # print("默认开票项目为：")
                # print(i)
                return i

    def get_store_list(self,token_pc,No=0):#获取门店id
        url=base_url_cloud+"/v3/client/client-store-list?token=%s"%token_pc
        print("获取门店请求url：{}".format(url))

        request_data={"token":token_pc}
        headers = {"Accept": "application/json, text/plain, */*", "Content-Type": "application/json;charset=UTF-8"}

        response=requests.post(url=url,json=request_data, headers=headers, verify=False)
        try:
            r=response.json()
            print(r)
            store_id=r['data']['list'][No]['store_id']
            return str(store_id)
            # print(store_id)
            # res = response.json()['data']['list']
            # print(res)
            # return res
        except :
            print("获取门店ID失败！！！")

    def get_client_id(self,tax_code):#获取商户id

        client_id=C.coonect_db(database="fpmerchant",sql="select id from m_sell_client_new where tax_code='%s'"%tax_code)
        # print(self.tax_code)
        c_id=str(client_id[0][0])
        # print(type(c_id))
        return c_id

    def get_equipment(self,token_pc,No=0):#获取税盘

        url=base_url_cloud+"/v3/equipment/list?token=%s"%token_pc
        print("获取税盘请求url：{}".format(url))

        request_data={"provider":"","type":"","number":"","page":1,"page_size":10}
        headers = {"Accept": "application/json, text/plain, */*", "Content-Type": "application/json;charset=UTF-8"}

        response=requests.post(url=url,json=request_data, headers=headers, verify=False)
        r=response.json()
        print(r)

        try:
            tax_num=r['data']['list'][No]['id']
            return str(tax_num)
        except:
            print("没有获取到税盘")
        # print(tax_num)

    def get_store_equipment(self,store_id,token_pc,invoice_style=0,No=0):#获取门店关联的税盘
        url = base_url_cloud+"/v3/client-store/get-tax-equipment?store_id=%s&invoice_style=%s&sid=&token=%s"%(store_id,invoice_style,token_pc)
        print("获取门店税盘id请求url：{}".format(url))
        headers = {"Accept": "application/json, text/plain, */*", "Content-Type": "application/json;charset=UTF-8"}

        response=requests.get(url=url, headers=headers, verify=False)
        r=response.json()
        print(r)

        try:
            tax_num_id=r['data'][0]['id']
            return str(tax_num_id)
        except:
            print("没有获取到税盘")

    def client_details(self,token_pc):#获取商户信息

        # print(token_pc)
        url = base_url_cloud + '/v3/client/get-client-info?sid=&token=%s'%token_pc
        headers = {"Accept": "application/json, text/plain, */*", "Content-Type": "application/json;charset=UTF-8"}
        response = requests.get(url=url, headers=headers, verify=False)
        res=response.json()['data']
        # print(res)
        return res
if __name__=="__main__":
    print(111)
    G=GetClient()
    tax_code="91140100751525588X"
    password="test123456"
    token_pc = GetToken().login_token(tax_code, password)
    # print(token_pc)
    # token_pc="HPXjciqfCM2nYv0W4WwOuIrjGEtIiWKPX10QEGmq"

    # print(G.project_details(token_pc))#获取开票项
    # print(G.project_details_default(token_pc))#获取默认开票项
    # print(G.get_store_list(No=0))
    # print(G.get_store_list(token_pc))#获取门店id
    # equipment=G.get_equipment(token_pc,No=0)
    # print(equipment)
    # print(type(equipment))#获取税盘

    # print(G.get_client_id(tax_code))#获取商户id
    print(G.client_details(token_pc=token_pc) )
